/*
*   @Author: 1296
*   @Date: 2025-05-05
*   @Description: 我的课程
*/
<template>
  <div class="course-container" v-if="courseList !== null">
    <div class="course-header">
      <h2>我的课程</h2>
    </div>

    <div class="course-cards">
      <el-row :gutter="20">
        <el-col :span="6" v-for="course in courseList" :key="course.id">
          <el-card
              class="course-card"
              shadow="hover"
              @click="openCourseDetails(course)"
          >
            <div class="course-card-image">
              <img :src="course.avatar || defaultAvatar" class="course-image" alt="CourseImage"/>
            </div>
            <div class="course-card-info">
              <h3 class="course-name">{{ course.name }}</h3>
            </div>
          </el-card>
        </el-col>
      </el-row>
    </div>

    <div class="pagination-container">
      <el-pagination
          :current-page="currentPage"
          :page-size="pageSize"
          :total="totalCourses"
          layout="prev, pager, next"
          @current-change="handlePageChange"
      ></el-pagination>
    </div>

    <!-- 课程详情对话框 -->
    <el-dialog
        v-model="showCourseDetails"
        width="60%"
        append-to-body
    >
      <template #header>
        <h2>课程详情</h2>
      </template>
      <div class="course-details-container">
        <div class="course-details-avatar">
          <img :src="selectedCourse?.avatar || defaultAvatar" class="course-avatar-large" alt="CourseImage"/>
        </div>
        <div class="course-details-info">
          <el-descriptions :column="1">
            <el-descriptions-item label="课程ID">{{ selectedCourse?.id }}</el-descriptions-item>
            <el-descriptions-item label="课程名称">{{ selectedCourse?.name }}</el-descriptions-item>
          </el-descriptions>
        </div>
      </div>
    </el-dialog>
  </div>

  <div class="empty-state" v-else>
    <div class="icon">
      <el-icon><FolderOpened /></el-icon>
    </div>
    <p>暂无课程</p>
  </div>
</template>

<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';
import {FolderOpened} from "@element-plus/icons-vue";
import store from "@/store";

const api = axios.create({
  baseURL: store.getters.getBaseURL
});

const example = require('@/assets/example.png')

// 添加请求和响应拦截器
api.interceptors.request.use(config => {
  const token = sessionStorage.getItem('userToken');
  if (token) {
    config.headers.Authorization = `Bearer ${token}`;
  }
  return config;
});

api.interceptors.response.use(
    response => response,
    error => {
      if (error.response?.status === 401) {
        window.location.href = '/loginAndRegister';
      }
      return Promise.reject(error);
    }
);

export default {
  components: {FolderOpened},
  data() {
    return {
      courseList: [],
      currentPage: 1,
      pageSize: 8,
      totalCourses: 0,
      showCourseDetails: false,
      selectedCourse: null,
      defaultAvatar: example
    };
  },
  created() {
    this.getCourseList();
  },
  methods: {
    // 获取课程列表
    getCourseList() {
      api
          .get(`/course/list?pageNo=${this.currentPage}&pageSize=${this.pageSize}`)
          .then(response => {
            if (response.data.status === 'success' && response.data.data !== null) {
              this.courseList = response.data.data.list;
              this.totalCourses = response.data.data.resultCount;
              if (this.totalCourses === undefined) {
                this.totalCourses = this.courseList.length;
              }
            } else if (response.data.data === null){
              this.courseList = null;
            } else {
              ElMessage.error(response.data.info || '获取课程列表失败');
            }
          })
          .catch(error => {
            console.error('Error fetching course list:', error);
            ElMessage.error('获取课程列表失败');
          });
    },
    // 打开课程详情对话框
    openCourseDetails(course) {
      this.selectedCourse = course;
      this.showCourseDetails = true;
    },
    // 处理分页变化
    handlePageChange(page) {
      this.currentPage = page;
      this.getCourseList();
    }
  }
};
</script>

<style scoped>
.course-container {
  padding: 20px;
}

.course-header {
  margin-bottom: 20px;
}

h2 {
  margin-bottom: 10px;
  color: #333;
}

.course-cards {
  margin-bottom: 30px;
}

.course-card {
  height: 100%;
  cursor: pointer;
  transition: all 0.3s;
  border-radius: 12px;
  overflow: hidden;
}

.course-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.1);
}

.course-card-image {
  height: 160px;
  width: 100%;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
}

.course-image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

.course-avatar-large {
  width: 150px;
  height: 150px;
  object-fit: cover;
  border-radius: 8px;
  margin-bottom: 20px;
}

.course-card-info {
  padding: 15px;
}

.course-name {
  margin: 0 0 10px 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.pagination-container {
  text-align: center;
  margin-top: 20px;
}

.course-details-container {
  display: flex;
  gap: 30px;
  align-items: flex-start;
}

.course-details-avatar {
  flex: 0 0 150px;
}

.course-details-info {
  flex: 1;
}

.el-descriptions {
  border: none;
}

.el-descriptions-item__content {
  color: #666;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 50px 0;
  color: #909399;
}

.icon {
  font-size: 60px;
  margin-bottom: 15px;
}

</style>